<?php
class Manager_RequestStatus
{
	const STATUS_NEW		= 'new';
	const STATUS_AWAITING	= 'awaiting';
	const STATUS_DONE		= 'done';
	const STATUS_REJECTED	= 'rejected';
	
	public static function initialize()
	{
		$types = array(self::STATUS_AWAITING, self::STATUS_DONE, self::STATUS_NEW, self::STATUS_REJECTED);
		
		$criteria = new Criteria();
		
		try {
			foreach ($types as $type) {
				$criteria->clear();
				$criteria->add(RequeststatusesPeer::NAME, $type, Criteria::EQUAL);
				
				if (RequeststatusesPeer::doCount($criteria) === 0) {
					// add to database missing reqest statuses
					$status = new Requeststatuses();
					$status->setName($type);
					$status->save();
				}
			}
		} catch (Exception $e) {
			throw new Exception('Request statuses could not be initialized in the DB. ' . $e->getMessage());
		}
			
	}
	
	/**
	 * 
	 * @param $name
	 * @return Requeststatuses
	 */
	public static function get($name)
	{
		$criteria = new Criteria();
		$criteria->add(RequeststatusesPeer::NAME, $name, Criteria::EQUAL);
		
		$status = RequeststatusesPeer::doSelectOne($criteria);
		
		if (empty($status)) {
			throw new Exception($name . ' <- status does not exists in the DB.');
		}
		
		return $status;
	}
	
	/**
	 * 
	 * @return Requeststatuses
	 */
	public static function getDone()
	{
		return self::get(self::STATUS_DONE);
	}
	
	public static function getNew()
	{
		return self::get(self::STATUS_DONE);
	}
	
	public static function getAwaiting()
	{
		return self::get(self::STATUS_AWAITING);
	}
	
	public static function getRejected()
	{
		return self::get(self::STATUS_REJECTED);
	}
}